package com.ocom.mqsimple.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.MqSimpleApiUrl;
import com.ocom.common.entity.card.EntityXfRecordProcess;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.payhandle.GetNotsendRecordRequest;
import com.ocom.common.request.simple.kcmz.KCMZConsumeRecordRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mqsimple.business.kcmz.impl.KCMZProductServiceImpl;
import com.ocom.mqsimple.feign.PayhandleClientService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class KCMZCTController {

    private final Logger logger = LoggerFactory.getLogger(KCMZCTController.class);

    @Autowired
    private KCMZProductServiceImpl productService;
    @Autowired
    private PayhandleClientService payhandleClientService;


    @PostMapping(MqSimpleApiUrl.URL_KcmzctMqConsumeRecord)
    public Result consumeRecord(@RequestBody KCMZConsumeRecordRequest request) {
        try {
            log.info("consumeRecord=====参数：【{}】", request);
            GetNotsendRecordRequest getNotsendRecordRequest = new GetNotsendRecordRequest();
            getNotsendRecordRequest.setCompanyId(request.getComId());
            getNotsendRecordRequest.setPrefix("SB");
            Result notsendRecord = payhandleClientService.getNotsendRecord(getNotsendRecordRequest);

            if (200 == notsendRecord.getCode() && null != notsendRecord.getData()) {
                List<EntityXfRecordProcess> getNosendRecordList = JSON.parseArray(JSON.toJSONString(notsendRecord.getData()), EntityXfRecordProcess.class);

                for (EntityXfRecordProcess recordProcess : getNosendRecordList) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("comId", recordProcess.getComId());
                    jsonObject.put("orderNo", recordProcess.getOrderNo());
                    log.info("KCMZProductService.handle=====参数：【{}】", jsonObject);
                    productService.handle(jsonObject.toJSONString());
                }
            }

        } catch (Exception e) {
            logger.error("发送mq失败  错误【{}】", e);
            return ResultUtil.error(ResultEnum.Err_4006);
        }
        return ResultUtil.success();
    }


}
